<?php
class User extends MultiLevel{
    protected $_name;
    protected $_primary = 'category_id';

    function __construct() {
        $this->_name = DB_PREFIX . "user";
        parent::__construct(); 
    }

    public function getColumns() {
        //$results = array('category_id', 'admin_title', 'parent', 'left', 'right', 'status');
        $results = array('category_id', 'admin_title', 'parent', 'left', 'right', 'demo', 'status', 'main_image', 'languageID', 'short_description', 'description', 'friendly_url', 'main_menu', 'template_id');
        return $results;
    }

    public function getPrimaryKey() {
        $pk = $this->_primary;
        if (is_array($pk)) {
            if (count($pk) > 0)
                return $pk[1]; // array[1] contain column name of primary key
        }
        return $this->_primary;
    }

    /**
     * Get number records of table...
     *
     * @return int
    public function getTotalRecords($where = null, $cond = null) {
        $select = Zend_Db_Table_Abstract::select();
        $select->from(array('thistable' => $this->_name), array('cnt' => "count('*')"));
        if ($where) {
            if (is_array($cond))
                $select->where($where, $cond);
            else $select->where($where);
        }

        $result = 0;
        $data = $this->fetchRow($select);
        @$result = $data->cnt;

        return $result;
    }
     */

    function getMenuRecords($order = 'node.left') {

        $table_name = $this->_name;
        $primary_key = $this->_primary;
        if($primary_key)
        $primary_key = $primary_key[1];

        $select = Zend_Db_Table_Abstract::select();
        //        ->setIntegrityCheck(false)
        $select
        ->from(array('node' => $this->_name))
        ->join(array('parent'=> $this->_name), "node.left BETWEEN parent.left AND parent.right", array('depth' => 'COUNT(parent.left) -1'))
        ->group("node.$primary_key")
        ->order($order);

        //echo $select;
        $data = $this->fetchAll($select);
        //$stmt = $select->query();
        //$data = $stmt->fetchAll();

        return $data;
    }
    
	public function getRecords($where = null, $order = 'node.left ASC', $count = null, $offset = 0)
    {
        $table_name = $this->_name;
        $primary_key = $this->_primary;
        if(is_array($primary_key))
        $primary_key = $primary_key[1];

        $select = Zend_Db_Table_Abstract::select();
        //        ->setIntegrityCheck(false)
        $select->setIntegrityCheck(false)
        ->from(array('node' => $this->_name))
        ->join(array('parent'=> $this->_name), "node.left BETWEEN parent.left AND parent.right", array('level' => 'COUNT(parent.left) -1'))
        //->joinLeft(array('img'=> DB_PREFIX . 'image'), "img.category_id = node.category_id", array('image_path'))
        //->joinLeft(array('level'=> DB_PREFIX . 'level'), "level.level_id = node.level_id", array('level_name'))
        ->group("node.$primary_key")
        ->order($order);
        if($where) {
            $select->where($where);
        }
        if($order) {
            $select->order($order);
        }
        if($count)
            $select->limit($count, $offset);
        //echo $select;
        //$data = $this->fetchAll($select);
        $stmt = $select->query();
        $data = $stmt->fetchAll();

        return $data;
    }
    
	public function getAllRecords($where = null, $order = 'thistable.left ASC', $count = null, $offset = 0)
    {
        $table_name = $this->_name;
        $primary_key = $this->_primary;
        if(is_array($primary_key))
        $primary_key = $primary_key[1];

        $select = Zend_Db_Table_Abstract::select();
        //        ->setIntegrityCheck(false)
        $select->setIntegrityCheck(false)
        ->from(array('thistable' => $this->_name))
        ->join(array('parent'=> $this->_name), "thistable.left BETWEEN parent.left AND parent.right", array('level' => 'COUNT(parent.left) -1'))
        //->joinLeft(array('img'=> DB_PREFIX . 'image'), "img.category_id = thistable.category_id", array('image_path'))
        ->joinLeft(array('level'=> DB_PREFIX . 'level'), "level.level_id = thistable.level_id", array('level_name'))
        ->group("thistable.$primary_key")
        ->order($order);
        if($where) {
            $select->where($where);
        }
        if($order) {
            $select->order($order);
        }
        if($count)
            $select->limit($count, $offset);
        //echo $select;
        //$data = $this->fetchAll($select);
        $stmt = $select->query();
        $data = $stmt->fetchAll();

        return $data;
    }
    /**
     * Get number records of table...
     *
     * @return int
     */
    public function getTotalRecords($where = null, $cond = null) {
        $table_name = $this->_name;
        $primary_key = $this->_primary;
        if(is_array($primary_key))
        $primary_key = $primary_key[1];


        $select = Zend_Db_Table_Abstract::select();
        $select->from(array('thistable' => $this->_name), array('cnt' => "count('*')"));
        $select->setIntegrityCheck(false)
        //->from(array('thistable' => $this->_name))
        //->join(array('parent'=> $this->_name), "thistable.left BETWEEN parent.left AND parent.right", array('level' => 'COUNT(parent.left) -1'))
        //->joinLeft(array('img'=> DB_PREFIX . 'image'), "img.category_id = thistable.category_id", array('image_path'))
        //->joinLeft(array('level'=> DB_PREFIX . 'level'), "level.level_id = thistable.level_id", array('level_name'))
        //->group("thistable.$primary_key")
        //->order($order)
        ;
        
        if($where) {
            $select->where($where);
        }
        /*
        if ($where) {
            if (is_array($cond))
                $select->where($where, $cond);
            else $select->where($where);
        }
        */
        
        //echo $select;

        $result = 0;
        $data = $this->fetchRow($select);
        @$result = $data->cnt;

        return $result;
    }
    public function getTableName()
    {
        return $this->_name;
    }

    public function isPassOk($user,$pass)
    {
        $where[] = $this->_db->quoteInto("user_name = ?", $user);
        $where[] = $this->_db->quoteInto("password = ?" , $pass);
        $data = $this->fetchAll($where);
        if (count($data->toArray())>0)
            return true;
        return false;
    }
}
?>
